<?php

if (isset($_GET["f"])) {
    $f = $_GET["f"];
    if ($f = "loadImages") {
        loadImages();
    }
}

function loadImages()
{
    if (!class_exists('Db')) {
        require __DIR__ . '/../init.php';
    }
    $db = Db::getInstance();
    $image_count = $db->queryColumn("select count(1) from image");
    $page_size = 18;
    $page = get('page', 1);
    $page = get_correct_page($page, $image_count, $page_size);
    $offset = $page >= 1 ? ($page - 1) * $page_size : 0;
    $images = $db->queryAll("select id,path,upload_time from image order by id desc limit $offset,$page_size");
    $pagination = pagination('', $image_count, $page_size, $page);

    echo $pagination;
    echo "<div class='row' style='margin:0;'>";
    foreach ($images as $it) {
        $image_id = $it['id'];
        $image = ROOTPATH . Option::get('UPLOAD_PATH', '/uploads') . $it['path'];
        $image_url = get_url(Option::get('UPLOAD_PATH', '/uploads') . $it['path']);
        $image_pathinfo = pathinfo($image);
        $image_extension = $image_pathinfo['extension'];

        $size = getimagesize($image);
        $image_height = $size[0];
        $file_size_byte = filesize($image);
        $file_size_kilobyte = ($file_size_byte / 1024);
        $file_size_kilobyte_rounded = round($file_size_kilobyte, 1);
        $filesizetemp = $file_size_kilobyte_rounded;

        ?>
        <div class="fileDiv"
             onclick="showEditBar('<?php echo $image_url; ?>','<?php echo $image_height; ?>','<?php echo $image_id; ?>');"
             ondblclick="showImage('<?php echo $image_url; ?>','<?php echo $image_height; ?>');"
             data-imgid="<?php echo $image_id; ?>">
            <div class="imgDiv"><img class="fileImg lazy" data-original="<?php echo $image_url; ?>"></div>
            <p class="fileDescription"><span
                    class="fileMime"><?php echo $image_extension; ?></span> <?php echo ltrim($it['path'], '/'); ?></p>
            <p class="fileTime"><?php echo $it['upload_time']; ?></p>
            <p class="fileTime"><?php echo $filesizetemp; ?> KB</p>
        </div>
        <?php

    }
    echo "</div>";
    echo $pagination;
}
